package com.yy.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Component;

import com.yy.dao.ICompanyDAO;
import com.yy.model.TbCompany;

@Component
public class CompanyDAO implements ICompanyDAO{
	@Resource
	private SessionFactory sessionFactory;
	
	@SuppressWarnings("unchecked")
	@Override
	public List<TbCompany> getAllCompany() throws Exception{
		String hql = "SELECT tbcompany FROM TbCompany as tbcompany";
		Query query = this.sessionFactory.getCurrentSession().createQuery(hql);
		return query.list();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public int getCompanyCount() throws Exception{
		int result = 0;
		try {
			Session session = sessionFactory.getCurrentSession();
			String hql = "select count(*) from TbCompany  ";
			Query query = session.createQuery(hql);
			result = Integer.parseInt(query.uniqueResult().toString());
		} catch (Exception e) {
			e.printStackTrace();
			result = 0;
		}
		return result;
		
	}
	@SuppressWarnings("unchecked")
	@Override
	public String getCompanyName(int companyId) throws Exception{
		//String ret=null;
		//try{
			//Session session = sessionFactory.getCurrentSession();
			String hql = "SELECT companyName FROM TbCompany WHERE companyId="+companyId;
			Query query = this.sessionFactory.getCurrentSession().createQuery(hql);
			return query.uniqueResult().toString();
	//	}
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<TbCompany> CompanyLogin(String comLogin) throws Exception {
		// TODO Auto-generated method stub
		List<TbCompany> result=null;
		try{
			Session session = sessionFactory.getCurrentSession();
			//System.out.println("this is dao session+"+session);
			String hql = "select tbcompany from TbCompany as tbcompany where companyManName= '" + comLogin+"'";
			//String hql = "from TbUser where userLoginName='" + userLoginName
			//		+ "' ";
			System.out.println(hql);
					//+ "' and userIsDeleted = 0";
			Query query = session.createQuery(hql);
			result = query.list(); 
		}catch(Exception e)
		{
			e.printStackTrace();
		}
		return result;
	}
	
}
